package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.adapter2.HuaqianAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Consume;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;


/**
 * 2018/3/14.
 */

public class Fragmenthua extends Fragment{
    private  int startindex=0;
    private  int lengthindex=1;
    private   XRecyclerView  xRecyclerView;
    private HuaqianAdapter huaqianAdapter;
    private   List<Consume.DataBean> lists = new ArrayList<>();

    private  int lodingint  ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.item,null);
       xRecyclerView = (XRecyclerView) view.findViewById(R.id.shengqian_xrecycle);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingMoreEnabled(true);
      //  xRecyclerView.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.HORIZONTAL));
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        xRecyclerView.setLayoutManager(manager);
    //    List<Consume.DataBean> dataBeans = getnetMingxi(startindex, lengthindex);
        if (huaqianAdapter == null){
            huaqianAdapter = new HuaqianAdapter(getActivity(), lists);
            xRecyclerView.setAdapter(huaqianAdapter);
        }else {
            huaqianAdapter.notifyDataSetChanged();
        }

        getnetMingxi(startindex, lengthindex);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lists.clear();
                        lodingint=0;
                        startindex=0;
                        lengthindex=1;
                      getnetMingxi(startindex, lengthindex);
                        huaqianAdapter.setData(lists);
                       huaqianAdapter.notifyDataSetChanged();
                        xRecyclerView.refreshComplete();
                    }
                },1000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                           lodingint=0;
                            startindex++;
                            lengthindex++;
                           getnetMingxi(startindex,lengthindex);
                            xRecyclerView.loadMoreComplete();
                    }
                },1000);
            }
        });

        return view;
    }




    private  List<Consume.DataBean> getnetMingxi(int startindex,int lengthindex) {
        String url = ApiManager.ConsumeLOG+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity())+"&start="+startindex+"&length="+lengthindex;
        Log.e("hua钱记录","主题url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("shenglog",responseInfo.result);
                Consume consume = new Gson().fromJson(responseInfo.result, Consume.class);
                String code = consume.getCode();
                if (code.equals("10000")){
                    List<Consume.DataBean> data = consume.getData();
                    lists.addAll(data);
                    if (lodingint==0){
                        huaqianAdapter.setData(data);
                    }else {
                        huaqianAdapter.setData(data);
                    }


                }else {
                    Toast.makeText(getActivity(), consume.getMsg(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
        return  lists;
    }


}

